package gps;

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.*;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;

/**
 *
 */
public class Dinges extends Form implements CommandListener {

  private GpsMIDlet midlet;
  public static TextField lat;
  public static TextField lon;
  public static TextField alt;
  public static TextField spd;
  public static TextField spdkmh;
  public static LocationProvider locationProvider;
  public static TextField tijd;
  /**
   * constructor
   */
  public Dinges(GpsMIDlet midlet) {
    super("GPS Dinges");
    this.midlet = midlet;
    StringItem koekje = new javax.microedition.lcdui.StringItem("GPS Dinges", "Goed he XD");
    append(koekje);
    lat = new TextField("Latitude: ", "", 30, TextField.ANY);
    append(lat);
    lon = new TextField("Longitude: ", "", 30, TextField.ANY);
    append(lon);
    alt = new TextField("Altitude: ", "", 30, TextField.ANY);
    append(alt);
    spd = new TextField("Speed: ", "", 30, TextField.ANY);
    append(spd);
    spdkmh = new TextField("Speed: ","",30, TextField.ANY);
    append(spdkmh);
    tijd = new TextField("Tijd: ","",30,TextField.ANY);
    append(tijd);
    createLocationProvider();
    ///Timer
    Timer timer = new Timer();
    TestTimerTask taak = new TestTimerTask();
    //timer.schedule(taak, 1000);
    timer.scheduleAtFixedRate(taak, 1000, 1000);
    ///Einde Timer

    try {
      // Set up this form to listen to command events
      setCommandListener(this);
      // Add the Exit command
      addCommand(new Command("Exit", Command.EXIT, 1));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  void createLocationProvider() {
    if (locationProvider == null) {
      Criteria criteria = new Criteria();
      try {
        locationProvider = LocationProvider.getInstance(criteria);
      } catch (Exception le) {
        lat.setString("GPS Werkt niet......");
        System.out.println("Cannot create LocationProvider for this criteria.");
        le.printStackTrace();
      }
    }
  }

  public class TestTimerTask extends TimerTask {

    public final void run() {
      //DE DINGEN DOEN
      /////////
      Location lastKnown = LocationProvider.getLastKnownLocation();
      System.out.println("-_-_-_-_-_-_-_-");
      System.out.println("lastKnown: " + lastKnown);
      System.out.println("locationProvider.getState: " + locationProvider.getState());
      tijd.setString(Long.toString(java.lang.System.currentTimeMillis()));
      try {
        Location getLocation5 = locationProvider.getLocation(5);
        System.out.println("getLocation5: " + getLocation5);
        System.out.println("getLocation5.getExtraInfo: " + getLocation5.getExtraInfo("text/plain"));
        System.out.println("getLocation5.toString: " + getLocation5.toString());
        System.out.println("getLocation5.getLocationMethod: " + getLocation5.getLocationMethod());
        System.out.println("getLocation5.getSpeed: " + getLocation5.getSpeed());
        String speedStr = ""+Float.toString(getLocation5.getSpeed())+" m/s";
        System.out.println("speedStr: " + speedStr);
        spd.setString(speedStr);
        spdkmh.setString("" + Double.toString((getLocation5.getSpeed())*3.6)+" km/h");
        System.out.println("getLocation5.isValid: " + getLocation5.isValid());
        QualifiedCoordinates cor = getLocation5.getQualifiedCoordinates();
        System.out.println("cor: " + cor);
        System.out.println("cor.getLatitude: " + cor.getLatitude());
        lat.setString(""+cor.getLatitude());
        System.out.println("cor.getLongitude: " + cor.getLongitude());
        lon.setString(""+cor.getLongitude());
        System.out.println("cor.getHorizontalAccuracy: " + cor.getHorizontalAccuracy());
        System.out.println("cor.getVerticalAccuracy: " + cor.getVerticalAccuracy());
        System.out.println("cor.getAltidude: " + cor.getAltitude());
        alt.setString("" + cor.getAltitude());
        System.out.println("-_-_-_-_-_-_-_-");
      } catch (Exception ex) {
        ex.printStackTrace();
      }
      ////////////
    }
  }

  public void commandAction(Command command, Displayable displayable) {
    if (command.getCommandType() == Command.EXIT) {
      midlet.destroyApp(true);
    }
  }
}
